#include "food_list_3.h"
#include "ui_food_list_3.h"
#include <QMessageBox>
#include <QMouseEvent>
#include<QDebug>
#include <QFont>

Food_list_3::Food_list_3(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Food_list_3)
{
    ui->setupUi(this);
    m_shop = new Shop_3;

    ui->label_10->setStyleSheet("color: red; font-size: 16px; font-family: Arial;");
    QFont font;
    //    font.setFamily("Arial"); // 设置字体家族为Arial
    //    font.setPointSize(16);   // 设置字体大小为16点
    font.setBold(true);      // 设置字体为粗体
    ui->label_10->setFont(font);
    ui->label_10->setAlignment(Qt::AlignCenter); // 设置文本居中

    ui->label_11->setStyleSheet("color: red; font-size: 16px; font-family: Arial;");
    ui->label_11->setFont(font);
    ui->label_11->setAlignment(Qt::AlignCenter); // 设置文本居中

    ui->label_12->setStyleSheet("color: red; font-size: 16px; font-family: Times New Roman;");
    ui->label_12->setFont(font);
    ui->label_12->setAlignment(Qt::AlignCenter); // 设置文本居中

    // 安装事件过滤器
    this->installEventFilter(this);

    QPixmap pix1_1(":/res/4-1.jpg");
    pix1_1 = pix1_1.scaled(400,210);
    ui->lb_10->setPixmap(pix1_1);
    ui->lb_10->installEventFilter(this); // 安装事件过滤器到lb_10

    QPixmap pix1_2(":/res/4-2.png");
    pix1_2 = pix1_2.scaled(400,210);
    ui->lb_11->setPixmap(pix1_2);
    ui->lb_11->installEventFilter(this); // 安装事件过滤器到lb_11

    QPixmap pix1_3(":/res/4-3.png");
    pix1_3 = pix1_3.scaled(400,210);
    ui->lb_12->setPixmap(pix1_3);
    ui->lb_12->installEventFilter(this); // 安装事件过滤器到lb_12

    connect(m_shop, &Shop_3::sendInt, this,
            &Food_list_3::recvInt);

    connect(this,&Food_list_3::sendStr,m_shop,
                    &Shop_3::recvStr);

    connect(this,&Food_list_3::sendInt,m_shop,
                    &Shop_3::recvInt);
    connect(this,&Food_list_3::sendInt1,m_shop,
                    &Shop_3::recvInt1);
    connect(this,&Food_list_3::sendInt2,m_shop,
                    &Shop_3::recvInt2);
    connect(this,&Food_list_3::sendInt3,m_shop,
                    &Shop_3::recvInt3);


}

Food_list_3::~Food_list_3()
{
    delete ui;
}

void Food_list_3::on_bt_2_clicked()
{
    int i = 1;
    this->close();
    emit sendInt(i);
}

void Food_list_3::recvInt(int i)
{
    if( i == 1 )
    {
        this->show();
    }
}

void Food_list_3::recvStr(QString str)
{
    emit sendStr(str);

}

void Food_list_3::on_bt_clicked()
{
    int ret = 0;
    ret = QMessageBox::question(this, "询问", "你确定要退出吗",
                                QMessageBox::No | QMessageBox::Yes,
                                QMessageBox::No);
    if(ret == QMessageBox::Yes)
    {
        int i = 2;
        emit sendInt(i);
        this->close();

    }
}


bool Food_list_3::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        if (mouseEvent->button() == Qt::LeftButton)
        {
            if (obj == ui->lb_10)
            {
                num +=19;
                num1++;
                ui->le_3->setText(QString::number(num1));
            }
            else if (obj == ui->lb_11)
            {
                num2++;
                num += 17;
                ui->le_1->setText(QString::number(num2));
            }
            else if (obj == ui->lb_12)
            {
                num3++;
                num += 18;
                ui->le_2->setText(QString::number(num3));
            }
            return true; // 表明事件已被处理
        }
        if (mouseEvent->button() == Qt::RightButton)
        {
            if (obj == ui->lb_10)
            {
                if(num1>0)
                {
                    num -=19;
                    num1--;
                    ui->le_3->setText(QString::number(num1));
                }

            }
            else if (obj == ui->lb_11)
            {
                if(num2>0)
                {
                    num2--;
                    num -= 17;
                    ui->le_1->setText(QString::number(num2));
                }
            }
            else if (obj == ui->lb_12)
            {
                if(num3>0)
                {
                    num3--;
                    num -= 18;
                    ui->le_2->setText(QString::number(num3));
                }
            }
            return true; // 表明事件已被处理
        }
    }
    ui->le_4->setText(QString::number(num));
    // 如果事件不是鼠标左键点击，或者不是针对我们感兴趣的QLabel，则继续正常的事件处理
    return QObject::eventFilter(obj, event);

}

void Food_list_3::on_bt_1_clicked()
{
    if(num != 0)
    {
        int ret = 0;
        ret = QMessageBox::question(this, "询问", "你确定要付款吗",
                                    QMessageBox::No | QMessageBox::Yes,
                                    QMessageBox::Yes);
        if(ret == QMessageBox::Yes)
        {
            emit sendInt(num);
            emit sendInt1(num1);
            emit sendInt2(num2);
            emit sendInt3(num3);
            qDebug() << "num:" <<num;
            qDebug() << "num1:" <<num1;
            qDebug() << "num2:" <<num2;
            qDebug() << "num3:" <<num3;
            m_shop->show();
            this->hide();
        }
    }
    else
    {
        QMessageBox::information(this, "通知", "请先下单");

    }

}

void Food_list_3::on_bt_3_clicked()
{
    QMessageBox::information(this, "帮助", "鼠标左键点击图片选择，右击取消");
}
